<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="准驾车型" prop="allowableType">
      <el-input v-model="dataForm.allowableType" placeholder="准驾车型"></el-input>
    </el-form-item>
    <el-form-item label="初次领证日期" prop="initialCertificateDate">
      <el-input v-model="dataForm.initialCertificateDate" placeholder="初次领证日期"></el-input>
    </el-form-item>
    <el-form-item label="有效期限" prop="validPeriod">
      <el-input v-model="dataForm.validPeriod" placeholder="有效期限"></el-input>
    </el-form-item>
    <el-form-item label="驾驶证号" prop="licenseNumber">
      <el-input v-model="dataForm.licenseNumber" placeholder="驾驶证号"></el-input>
    </el-form-item>
    <el-form-item label="驾龄" prop="driverAge">
      <el-input v-model="dataForm.driverAge" placeholder="驾龄"></el-input>
    </el-form-item>
    <el-form-item label="驾驶证类型" prop="licenseType">
      <el-input v-model="dataForm.licenseType" placeholder="驾驶证类型"></el-input>
    </el-form-item>
    <el-form-item label="从业资格证信息" prop="qualificationCertificate">
      <el-input v-model="dataForm.qualificationCertificate" placeholder="从业资格证信息"></el-input>
    </el-form-item>
    <el-form-item label="入场证信息" prop="passCertificate">
      <el-input v-model="dataForm.passCertificate" placeholder="入场证信息"></el-input>
    </el-form-item>
    <el-form-item label="图片" prop="picture">
      <el-input v-model="dataForm.picture" placeholder="图片"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          allowableType: '',
          initialCertificateDate: '',
          validPeriod: '',
          licenseNumber: '',
          driverAge: '',
          licenseType: '',
          qualificationCertificate: '',
          passCertificate: '',
          picture: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          allowableType: [
            { required: true, message: '准驾车型不能为空', trigger: 'blur' }
          ],
          initialCertificateDate: [
            { required: true, message: '初次领证日期不能为空', trigger: 'blur' }
          ],
          validPeriod: [
            { required: true, message: '有效期限不能为空', trigger: 'blur' }
          ],
          licenseNumber: [
            { required: true, message: '驾驶证号不能为空', trigger: 'blur' }
          ],
          driverAge: [
            { required: true, message: '驾龄不能为空', trigger: 'blur' }
          ],
          licenseType: [
            { required: true, message: '驾驶证类型不能为空', trigger: 'blur' }
          ],
          qualificationCertificate: [
            { required: true, message: '从业资格证信息不能为空', trigger: 'blur' }
          ],
          passCertificate: [
            { required: true, message: '入场证信息不能为空', trigger: 'blur' }
          ],
          picture: [
            { required: true, message: '图片不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/base/truckdriverlicense/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.truckDriverLicense.userId
                this.dataForm.allowableType = data.truckDriverLicense.allowableType
                this.dataForm.initialCertificateDate = data.truckDriverLicense.initialCertificateDate
                this.dataForm.validPeriod = data.truckDriverLicense.validPeriod
                this.dataForm.licenseNumber = data.truckDriverLicense.licenseNumber
                this.dataForm.driverAge = data.truckDriverLicense.driverAge
                this.dataForm.licenseType = data.truckDriverLicense.licenseType
                this.dataForm.qualificationCertificate = data.truckDriverLicense.qualificationCertificate
                this.dataForm.passCertificate = data.truckDriverLicense.passCertificate
                this.dataForm.picture = data.truckDriverLicense.picture
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/base/truckdriverlicense/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'allowableType': this.dataForm.allowableType,
                'initialCertificateDate': this.dataForm.initialCertificateDate,
                'validPeriod': this.dataForm.validPeriod,
                'licenseNumber': this.dataForm.licenseNumber,
                'driverAge': this.dataForm.driverAge,
                'licenseType': this.dataForm.licenseType,
                'qualificationCertificate': this.dataForm.qualificationCertificate,
                'passCertificate': this.dataForm.passCertificate,
                'picture': this.dataForm.picture
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
